package thread;

import java.util.concurrent.CountDownLatch;

public class Demo32 {
    public static void main(String[] args) throws InterruptedException {
        //模拟跑步比赛
        //构造方法中设定有几个选手参赛
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(()->{
                try {
                    Thread.sleep(3000);
                    System.out.println("到达终点!");
                    //countDown相当于撞线
                    latch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
        //await在等所有线程都撞线
        //调用countDown的次数达到初始化的时候达到的设定值
        //await 就返回,  否则await就阻塞等待
        latch.await();
        System.out.println("比赛结束!");
    }
}
